<template>
    <div class="app-container">
        <!-- Header 头部区域 -->
        <Header title="购物车案例"></Header>
        <!-- 循环渲染每一个商品的信息 -->
        <Goods
                v-for="item in list"
                :key="item.id"
                :id="item.id"
                :title="item.goods_name"
                :pic="item.goods_img"
                :price="item.goods_price"
                :state="item.goods_state"
                @state-change="getNewState"
        >
            <Counter :num="item.goods_count" @num-change="getNewNum(item,$event)"></Counter>
        </Goods>

        <!-- Footer 区域 -->
        <Footer :isfull="fullState" :amount="amt" :all="total" @full-change="getFullState"></Footer>
    </div>
</template>

<script>
// 导入 axios 请求库
import axios from 'axios'
// 导入需要的组件
import Header from '@/components/Header/Header.vue'
import Goods from '@/components/Goods/Goods.vue'
import Footer from '@/components/Footer/Footer.vue'
import Counter from "@/components/Counter/Counter.vue";

export default {
    data() {
        return {
            // 用来存储购物车的列表数据，默认为空数组
            list: []
        }
    },
    // 计算属性
    computed: {
        // 动态计算出全选的状态是 true 还是 false
        fullState() {
            return this.list.every(item => item.goods_state)
        },
        // 已勾选商品的总价格
        amt() {
            // 1. 先 filter 过滤
            // 2. 再 reduce 累加
            return this.list
                .filter(item => item.goods_state)
                .reduce((total, item) => (total += item.goods_price * item.goods_count), 0)
        },
        // 已勾选商品的总数量
        total() {
            return this.list.filter(item => item.goods_state).reduce((t, item) => (t += item.goods_count), 0)
        }
    },
    created() {
        // 调用请求数据的方法
        this.initCartList()
    },
    methods: {
        getNewNum(item,e) {
            item.goods_count = e
        },
        // 封装请求列表数据的方法
        async initCartList() {
            // // 调用 axios 的 get 方法，请求列表数据
            // const { data: res } = await axios.get('https://www.escook.cn/api/cart')
            // // 只要请求回来的数据，在页面渲染期间要用到，则必须转存到 data 中
            // if (res.status === 200) {
            //   this.list = res.list
            // }
            this.list = [
                {
                    "id": 1,
                    "goods_count": 1,
                    "goods_img": "http://t14.baidu.com/it/u=43462758,1432062369&fm=224&app=112&f=JPEG?w=500&h=500",
                    "goods_name": "辰米范 法式收腰连衣裙女显瘦新款设计感露背黑色v领泡泡袖长裙子",
                    "goods_price": 188.00,
                    "goods_state": false
                },
                {
                    "id": 2,
                    "goods_count": 1,
                    "goods_img": "https://img14.360buyimg.com/imgzone/jfs/t1/195902/5/25856/287194/62b36d0bEbd86d5b8/df3db4bd5bc056a9.jpg",
                    "goods_name": "质感至上 精致贵女风 金属丝星云升级版日本三醋酸连衣裙",
                    "goods_price": 998.00,
                    "goods_state": false
                },
                {
                    "id": 3,
                    "goods_count": 2,
                    "goods_img": "https://inews.gtimg.com/newsapp_bt/0/8111639339/1000",
                    "goods_name": "高知疏离 高氧分小洋裙 空气感高包容重磅珍珠缎双层真丝",
                    "goods_price": 288.00,
                    "goods_state": true
                },
                {
                    "id": 4,
                    "goods_count": 1,
                    "goods_img": "http://t13.baidu.com/it/u=2480967665,3117449742&fm=224&app=112&f=JPEG?w=500&h=500",
                    "goods_name": "好养眼 金高银式素美人气质 优雅生姿 竹节轻麻感空气裙",
                    "goods_price": 198.00,
                    "goods_state": true
                },
                {
                    "id": 5,
                    "goods_count": 1,
                    "goods_img": "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F0c873895-112e-4286-b797-9f0fdae3dff2%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1692368179&t=7d482246a02b7bafa81aff12d8e36afe",
                    "goods_name": "女童连衣裙2023新款夏季儿童泡泡袖裙子女孩蓬蓬公主裙礼服童装夏",
                    "goods_price": 79.00,
                    "goods_state": true
                }
            ]
        },
        // 接收子组件传递过来的数据
        // e 的格式为 { id, value }
        getNewState(e) {
            this.list.some(item => {
                if (item.id === e.id) {
                    item.goods_state = e.value
                    // 终止后续的循环
                    return true
                }
            })
        },
        // 接收 Footer 子组件传递过来的全选按钮的状态
        getFullState(val) {
            this.list.forEach(item => (item.goods_state = val))
        }
    },
    components: {
        Header,
        Goods,
        Footer,
        Counter
    }
}
</script>

<style lang="less" scoped>
.app-container {
  padding-top: 45px;
  padding-bottom: 50px;
}
</style>
